#include <SDL.h>

#include "control.h"
#include "sprite.h"

Control g_control;

extern Sprite* g_control_sprite;

int GetControlColor(int x, int y);

void InitControl(SDL_Surface* surface) {
    g_control.width = 140;
    g_control.height = 140;
    g_control.buffer = calloc(g_control.width * g_control.height, sizeof(char));
    SDL_LockSurface(surface);

    int x = 140;
    int y = 240;
    char* pixels = (char*)surface->pixels;

    int i, j;
    for (j = 0; j < g_control.height; ++j)
        for (i = 0; i < g_control.width; ++i)
            *(g_control.buffer + j * g_control.width + i) =
                *(pixels + surface->w * (y + j) + x + i);

    SDL_UnlockSurface(surface);
}

void DestroyControl() {
    if (g_control.buffer)
        free(g_control.buffer);
}

int GetControlColor(int x, int y) {
    return *(g_control.buffer + y * g_control.width + x);
}

void DrawControl(int x, int y) {
    DrawSprite(g_control_sprite, x, y);
}
